/**
* @Title: institution_list_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-chenyonghong001
* @date 2014年10月17日 下午2:38:47
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('pharmacyListCtrl', ['$scope','CommunityService','$http','$state','InstitutionListService','BaseURL','Modal',
	                                function($scope,CommunityService,$http,$state,institutionListService,baseUrl,Modal){
		//判断是否已经查询过 药店信息  查询过则直接赋值
		if (institutionListService.pharmacyMedicalOrgInfoList) {
			 $scope.medicalOrgInfoList = institutionListService.pharmacyMedicalOrgInfoList;
		}
		else {
			institutionListService.initMedicalOrgInfoList("","","1").success(function(response){
	            $scope.medicalOrgInfoList = response.medicalOrgList;
	            institutionListService.pharmacyMedicalOrgInfoList = response.medicalOrgList;
			});
		}
        $scope.communityService = CommunityService;
        //	codeNo=350200&codeType=region&regionType=3
        CommunityService.getBaseCode('{codeType:\'regionXiamen\',codeNo:\'350200\',regionType:\'3\'}');
		CommunityService.initCodeListDatas();
        //  $scope.UrlParams = $routeParams;
        $scope.institutionListParam = {
            page:"1",
            pageSize:"20"
        };
    	$scope.currentTab = null;
		$scope.tabs = [  {name:'定点医院',type:'institutionInfo.institution'},
			             {name:'定点药店',type:'institutionInfo.pharmacy'}
		            ];
		
        //分页监听
        $scope.$on('pageTool.skipPage',function(e,v){
            $scope.onSubmit(v);
        });
		$scope.currentTab =$scope.tabs[1];
		// 表头以及json对应的属性名称
		$scope.columns = [ {
			label : '机构编号',
			map : 'providerCode',
			width : '15%'
		}, {
			label : '机构名称',
			map : 'providerName',
			width : '25%',
			 cellTemplate:'<a  ng:bind="data.providerName" '+
			 	'ui-sref="institutionInfo.phmaracyDetail({providerid:data.providerId,type:1})"></a>'
	    }, {
			label : '医保负责人',
			map : 'insuranceCharger',
			width : '15%'
		}, {
			label : '联系电话',
			map : 'chargerPhone',
			width : '20%'
		}, {
			label : '地址',
			map : 'fullAddress',
			width : '25%'
		}];
        $scope.institutionList={};
        if (institutionListService.pharmacyListParam) {
            $scope.institutionListParam = institutionListService.pharmacyListParam;
        }
        
        $scope.authority={
        		add:1,
        		update:1,
        		deleted:1
        }
        $scope.onSubmit = function(page){
          	if(!page){
        		page=1;
        	}
        	 $scope.institutionListParam.page = page;
          //  var pageInfo = $scope.pageInfo; 
        	$scope.institutionListParam.providertype = '1';
            var params = $scope.institutionListParam;
           institutionListService.pharmacyListParam = $scope.institutionListParam;
            institutionListService.query(params).success(function(res){
            	if(res){
            		$scope.tableData = res.result;
            		if(res.totals){
            			$scope.totalCount = res.totals.total;
            		}else{
            			$scope.totalCount = 0;
            		}
        			
        			$scope.authority=res.authority;
            	}
          
              //配置分页参数
//              $scope.pageInfo = {
//                  curPage:params.page,
//                  total:data.totals.total,
//                  limit: params.pageSize,
//                  loadData:$scope.queryInstitutionList
//              };
			});
        };

		$scope.addInstitution = function () {
			$state.go('institutionInfo.phmaracyDetail',{
				type:1
			});
		};
		
	
		//切换tab页
		$scope.tabClick = function(tab) {
			$state.go(tab.type);
		}
        /**
         * 机构详细信息--新增
         * @param params
         */
//        $scope.institutionDetail = {};
//        $scope.addProvider = function(){
//            var params = $scope.institutionDetail;
//            institutionListService.createProvider(params,function(data){
//                alert(data.msg);
//            });
//        };
        $scope.onReset=function(){
            $scope.institutionListParam.providerName="";
            $scope.institutionListParam.providerCode="";
            $scope.institutionListParam.addressCounty="";
        };
        /**
         * 列表复选框--全选控制
         * @type {{}}
         */
//        $scope.institutionIdList = "";
//        $scope.getInstitutionIdList = function(domName,isArray,prefix){
//            $scope.institutionIdList = Utils.getCheckBoxValue(domName,isArray,prefix);
//        };

        /**
         *机构列表--删除
         * @param params
         */
//        $scope.deleteInstitutionList = function(){
//          	dialog.confirm("确认要删除该医院信息吗？若删除，其相关的医疗设备、违规、联系信息、账号信息、诊疗项目、医生信息也将一并被删除！",function(){
//                var params = {ids:$scope.institutionIdList};
//                institutionListService.deleteInstitutionList(params,function(data){
//                	dialog.alert(data.msg);
//                	  $scope.institutionIdList ="";
//                      document.getElementById('institutionIdList').checked = false;
//                    $scope.queryInstitutionList($scope.agreementListParam);
//                });
//          	})
//        
//        };
		
		 var totalChecks = [];
	       $scope.checkRows = [];

	        function findCheck(d) {
	            for(var i=0;i< $scope.checkRows .length;i++) {
	                var checkedData =  $scope.checkRows [i];
	                if(checkedData.itemId == d.itemId) {
	                    return i;
	                }
	            }
	            return -1;
	        }

	        $scope.$on('fwatable.checked', function(e,d){
	            if(findCheck(d) == -1) {
	                $scope.checkRows.push(d);
	            }
	        });

	        $scope.$on('fwatable.unchecked', function(e,d){
	            var index = findCheck(d);
	            if(index != -1) {
	                $scope.checkRows.splice(index,1);
	            }
	        });
			
			
			$scope.deleteInstitution= function(){
			      var checkRows =$scope.checkRows;
		            if (checkRows.length == 0) {
		                Modal.alert("请勾选删除项！");
		                return;
		            }else
		            {
		                var ids = '';
		                angular.forEach(checkRows,function(totalCheck,i){
		                    ids+="'"+totalCheck.providerId+"',";
		                });
		                var  params= {
		                ids : ids
		                }
		            	Modal.confirm('确认删除所选择项？',function(){
							$http.post(baseUrl + '/hcpms/provider/delete.do',params).success(function(data) {
			                    if(data.state=='0')
			                    {
			                        Modal.alert(data.msg);
			                    }else
			                    {
			                        Modal.alert("删除失败！");
			                    }
			                    $scope.onSubmit();
			                })
						});
		            }
			}
		
			$scope.onSubmit();
		
    }
	]);
})();